package com.zte.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.market.dao.MarketOrderDetailDao;
import com.zte.market.dao.MarketOrdersDao;
import com.zte.market.model.MarketOrderdetails;
import com.zte.market.model.MarketOrders;
import com.zte.market.model.MarketOrderCondition;
import com.zte.market.service.MarketOrdersService;
import com.zte.sys.login.model.LUsers;
import com.zte.util.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Locale;

@Service
public class MarketOrdersServiceImpl extends ServiceImpl<MarketOrdersDao,MarketOrders> implements MarketOrdersService {
    private final Logger logger = LoggerFactory.getLogger(MarketOrdersServiceImpl.class);
    @Autowired
    private MarketOrdersDao marketOrdersDao;
    @Autowired
    private MarketOrderDetailDao orderDetailDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取订单信息，可通过权限信息以及用户id查询信息
     * 用于订单管理页面的查询分页操作
     * @param pageModel
     * @param condition
     * @return
     */
    @Override
    public PageModel<MarketOrders> pageOrdersInfoByCondition(PageModel<MarketOrders> pageModel, MarketOrderCondition condition) {
        LUsers lUsers=(LUsers) redisTemplate.opsForValue().get(String.valueOf(condition.getOperatorid()));
        if(lUsers.getJobId()==1||lUsers.getJobId()==2) {
            condition.setOperatorid(null);
        }
        condition.setCustomName(condition.getCustomName().toUpperCase(Locale.ROOT));
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        PageInfo<MarketOrders> pageInfo=new PageInfo<>(marketOrdersDao.queryOrdersByCondition(condition));
        return PageModel.getPageModel(pageModel,pageInfo);
    }

    /**
     * 获取订单信息
     * 用于订单审核时查询根据审核人查询
     * @param pageModel
     * @param condition
     * @return
     */
    @Override
    public PageModel<MarketOrders> pageOrderExamineInfoByCondition(PageModel<MarketOrders> pageModel, MarketOrderCondition condition) {
        condition.setCustomName(condition.getCustomName().toUpperCase(Locale.ROOT));
        PageHelper.startPage(pageModel.getPageNum(),pageModel.getPageSize());
        PageInfo<MarketOrders> pageInfo=new PageInfo<>(marketOrdersDao.queryOrdersByCondition(condition));
        return PageModel.getPageModel(pageModel,pageInfo);
    }

    //添加订单
    @Transactional
    @Override
    public boolean addOrders(MarketOrders orders) {
        try {
            marketOrdersDao.addOrder(orders);
            orders.getOrderdetails().forEach((item)->{
                orderDetailDao.addOrderDetailInfo(item);
            });
            return true;
        }catch (Exception e){
            logger.error(e.toString());
            return false;
        }
    }

    /**
     * 修改订单
     * @param orders
     * @return
     */
    @Transactional
    @Override
    public boolean updateOrder(MarketOrders orders) {
        try {
            //删除原有订单详情
            orderDetailDao.delete(new LambdaQueryWrapper<MarketOrderdetails>().eq(MarketOrderdetails::getOrderId, orders.getOrderid()));
            //更新现有订单数据
            marketOrdersDao.updateOrder(orders);
            //插入新订单详情数据
            orders.getOrderdetails().forEach((item) -> {
                orderDetailDao.addOrderDetailInfo(item);
            });
            return true;
        }catch (Exception e){
            logger.error(e.toString());
            return false;
        }
    }

    /**
     * 通过订单id查询订单数据
     * 用于查看详情以及修改回显
     * @param orderId
     * @return
     */
    @Override
    public MarketOrders getOrderInfoById(String orderId) {
        return marketOrdersDao.queryOrderById(orderId);
    }
}
